package com.homwee.tvap.common.comSettingInterface;

import android.content.Context;
import android.media.AudioManager;
import android.util.Log;

import com.mediatek.twoworlds.tv.MtkTvConfig;
import com.mediatek.twoworlds.tv.common.MtkTvConfigTypeBase;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//factory 4
public class factoryPage4 {
	static factoryPage4 Interface = null;
	MenuConfigManager mCfg = MenuConfigManager.getInstance(null);

	MtkTvConfig mMtkTvConfig = null;
	AudioManager audioManager = null;

	private factoryPage4(Context context) {

		try {
			audioManager = (AudioManager) context
					.getSystemService(Context.AUDIO_SERVICE);
			mMtkTvConfig = MtkTvConfig.getInstance();
		} catch (Exception e) {
			// TODO: handle exception
			Log.d("zcj", "audioManager ==null");
		}
	}

	public static factoryPage4 getInstance(Context context) {
		if (Interface == null) {
			Interface = new factoryPage4(context);
		}
		return Interface;
	}

	public int getVolume() {
		int volume = 30;
		if (audioManager != null) {
			return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		}
		return volume;
	}

	public int getBalance() {
		int balance = 0;
		if (mMtkTvConfig != null) {
			balance = mMtkTvConfig
					.getConfigValue(MtkTvConfigTypeBase.CFG_AUD_AUD_BALANCE);
		}
		return balance;
	}

	public void setVolume(int value) {
		if (audioManager != null) {
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value,
					AudioManager.FLAG_PLAY_SOUND);
			try {
				Class c = Class.forName("android.media.AudioManager");
				Method m = c.getMethod("disableSafeMediaVolume");
				m.invoke(audioManager);
			} catch (InvocationTargetException e) {
				e.printStackTrace();
				Log.e("CHFactory.setVolume", "InvocationTargetException :" + e.toString());
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
				Log.e("CHFactory.setVolume", "NoSuchMethodException :" + e.toString());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				Log.e("CHFactory.setVolume", "IllegalAccessException :" + e.toString());
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				Log.e("CHFactory.setVolume", "ClassNotFoundException :" + e.toString());
			}
		}
	}

	public void setBalance(int value) {
		if (mMtkTvConfig != null) {
			mMtkTvConfig.setConfigValue(
					MtkTvConfigTypeBase.CFG_AUD_AUD_BALANCE, value);
		}

	}

	public boolean updateDigitalAmpdata() {
		/*
		 * //if(mTvMiscManager !=null){ //return
		 * mTvMiscManager.updateDigitalAmpdata();
		 * //mTvMiscManager.updateDigitalAmpdata(); return true; }
		 */
		return false;
	}
}